package com.easyCode.dataMock.utils;

import net.datafaker.Faker;
import org.apache.commons.lang3.RandomStringUtils;
import org.jetbrains.annotations.NotNull;

import java.sql.Timestamp;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.concurrent.ThreadLocalRandom;

import static com.easyCode.dataMock.constants.RandomRuleConstants.*;

/**
 * 随机数生成工具
 */
public class FakerUtils {

    private final static Faker ZH_FAKER = new Faker(new Locale("zh-CN"));

    private final static Faker EN_FAKER = new Faker();

    private final static DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取随机值
     *
     * @param randomTypeEnum
     * @return
     */
    public static String getRandomValue(String[] randomTypeEnum) {
        String defaultValue = RandomStringUtils.randomAlphanumeric(2, 6);
        if (randomTypeEnum == null) {
            return defaultValue;
        }
        switch (randomTypeEnum[0]) {
            case NAME:
                return ZH_FAKER.name().name();
            case CITY:
                return ZH_FAKER.address().city();
            case EMAIL:
                return EN_FAKER.internet().emailAddress();
            case URL:
                return EN_FAKER.internet().url();
            case IP:
                return ZH_FAKER.internet().ipV4Address();
            case INT:
                return String.valueOf(getRandomInt(randomTypeEnum));
            case FLOAT:
                return String.valueOf(getRandomFloat(randomTypeEnum));
            case UNIVERSITY:
                return ZH_FAKER.university().name();
            case DATE:
                return getRandomDate(randomTypeEnum);
            case TIMESTAMP:
                return String.valueOf(EN_FAKER.date()
                        .between(Timestamp.valueOf("1900-01-01 00:00:00"), Timestamp.valueOf("2023-01-01 00:00:00"))
                        .getTime());
            case PHONE:
                return ZH_FAKER.phoneNumber().cellPhone();
            default:
                return defaultValue;
        }
    }

    @NotNull
    private static String getRandomDate(String[] randomTypeEnum) {
        if(randomTypeEnum.length==3){
            return EN_FAKER.date()
                    .between(Timestamp.valueOf(randomTypeEnum[1]), Timestamp.valueOf(randomTypeEnum[2]))
                    .toLocalDateTime().format(DATE_TIME_FORMATTER);
        }
        return EN_FAKER.date()
                .between(Timestamp.valueOf("1900-01-01 00:00:00"), Timestamp.valueOf("2023-01-01 00:00:00"))
                .toLocalDateTime().format(DATE_TIME_FORMATTER);
    }

    private static Double getRandomFloat(String[] randomTypeEnum) {
        if(randomTypeEnum.length==2){
            return ThreadLocalRandom.current().nextDouble(Double.parseDouble(randomTypeEnum[1]));
        }
        return randomTypeEnum.length==3 ? ThreadLocalRandom.current().nextDouble(Double.parseDouble(randomTypeEnum[1])
                ,Double.parseDouble(randomTypeEnum[2])):ThreadLocalRandom.current().nextDouble();
    }

    private static Integer getRandomInt(String[] randomTypeEnum) {
        if(randomTypeEnum.length==2){
            return ThreadLocalRandom.current().nextInt(Integer.parseInt(randomTypeEnum[1]));
        }
        return randomTypeEnum.length==3 ? ThreadLocalRandom.current().nextInt(Integer.parseInt(randomTypeEnum[1])
                ,Integer.parseInt(randomTypeEnum[2])):ThreadLocalRandom.current().nextInt();
    }
}